package com.google.code.basic.oop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class AlarmParser {
	private static final int NUMBER_OF_COLUMNS_IN_FILE = 6;
	private static final Logger logger = Logger.getLogger(AlarmParser.class);

	private Map<String, String> parseRow(String fileRow){
		Map<String, String> result = new HashMap<String, String>();
		String[] line = fileRow.split(";");
		result.put("alarmId", line[0]);
		result.put("alarmNumber", line[1]);
		result.put("severity", line[2]);
		result.put("alarmText",line[3]);
		result.put("additionalInfo", line[4]);
		result.put("alarmTime", line[5]);
		if (line.length > NUMBER_OF_COLUMNS_IN_FILE) {
			result.put("clearTime", line[6]);
		}
		return result;		
	}
	
	public static Date StringtoDate(String parsedTime, String format){
		
		if((parsedTime == null) || (format == null)){ return null; }
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date result = null;
		try {
			result = new Date(dateFormat.parse(parsedTime).getTime());
		} catch (ParseException e) {
			logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,e.getStackTrace().toString()));
		}
		return result;
	}
	
	private Alarm createAlarm(Map<String,String> alarms, String networkElement){
		Alarm result = new Alarm();
		for (Map.Entry<String, String> entry : alarms.entrySet()) {
			if(entry.getKey().equalsIgnoreCase("alarmId")) { result.setAlarmId(Integer.parseInt(entry.getValue())); }
			if(entry.getKey().equalsIgnoreCase("alarmNumber")) { result.setAlarmNumber(Integer.parseInt(entry.getValue())); }
			if(entry.getKey().equalsIgnoreCase("severity")) { result.setSeverity(Integer.parseInt(entry.getValue())); }
			if(entry.getKey().equalsIgnoreCase("alarmText")) { result.setAlarmText(entry.getValue()); }
			if(entry.getKey().equalsIgnoreCase("additionalInfo")) { result.setAdditionalInfo(entry.getValue()); }
			if(entry.getKey().equalsIgnoreCase("alarmTime")) { result.setAlarmTime(StringtoDate(entry.getValue(),"dd.MM.yyyy HH:mm")); }
			if(entry.getKey().equalsIgnoreCase("clearTime")) { result.setClearTime(StringtoDate(entry.getValue(),"dd.MM.yyyy HH:mm")); }
		}
		result.setSourceNe(networkElement);
		return result;
	}
	
	public List<Alarm> retrieveAlarmsFromFile(String fileName, String networkElement) {

		List<Alarm> result = new LinkedList<Alarm>();
		FileReader alarmfileReader = null;
		BufferedReader alarmBuffReader = null;
		try {
			alarmfileReader = new FileReader(new File(fileName));
			alarmBuffReader = new BufferedReader(alarmfileReader);
			String fileRow = "";
			Scanner inputScanner = new Scanner(alarmBuffReader);
	 		while (inputScanner.hasNextLine()) {
				fileRow = inputScanner.nextLine();
				result.add(createAlarm(parseRow(fileRow),networkElement)   );
			}
		} catch (FileNotFoundException e) {
			logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,e.getStackTrace().toString()));
		}
		finally {
			if(alarmBuffReader != null){
				try {
					alarmBuffReader.close();
				}
			 	catch (IOException ex) {
					logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,ex.getStackTrace().toString()));
			 	}
			}
			if(alarmfileReader != null){
				try {
					alarmfileReader.close();
				}
			 	catch (IOException ex) {
					logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,ex.getStackTrace().toString()));
			 	}
			}
		}
		return result;
	}

}


